package com.vercon.otamirror.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.vercon.commonmirror.utils.LogUtils;
import com.vercon.otamirror.utils.helper.FragmentOpenManager;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.jessyan.autosize.AutoSize;

/**
 * Created by csh on 2021/1/22
 */
public abstract class BaseFragment extends Fragment implements View.OnTouchListener {

    protected final String TAG = this.getClass().getSimpleName();
    private Unbinder mBind;
    private ViewGroup mViewGroup;
    private GestureDetector detector;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //detector = new GestureDetector(getContext(), gestureListener);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        AutoSize.autoConvertDensityOfGlobal(getActivity());
        View view = inflater.inflate(setContentView(savedInstanceState), container, false);
        mBind = ButterKnife.bind(this, view);
        //初始化
        initView(view);
        if(null != container){
            mViewGroup = container;
            //mPageBackView = new PageBackView(getContext());
            //container.addView(mPageBackView);
        }
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //防止点击穿透
        view.setOnTouchListener(this);
        //初始化监听
        initListener(view);
    }

    protected abstract int setContentView(@Nullable Bundle savedInstanceState);

    protected abstract void initView(View view);

    protected abstract void initListener(View view);

    //返回键
    public void onBack() {
        FragmentOpenManager.getInstance().onBack(getActivity());
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //detector.onTouchEvent(event);
//        if(null != mPageBackView){
//            mPageBackView.onTouchEvent(event);
//        }
        return true;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.d("onDestroy");
        if (mBind != null) {
            mBind.unbind();
        }
//        if(null != mViewGroup){
//            mViewGroup.removeView(mPageBackView);
//        }
    }

    public String getSdf() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date(System.currentTimeMillis()));
    }

    public void hideBottomMenu() {
        try {
            //全屏
            //for new api versions.
            View decorView = getActivity().getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }catch (Exception e){

        }

    }

    public void hideKeyboard(View view) {
        //隐藏键盘
        if (view != null && getContext() != null) {
            InputMethodManager im = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (im != null) {
                im.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        hideBottomMenu();
    }

    public void showKeyboard(View view) {
        //显示键盘
        if (view != null && getContext() != null) {
            InputMethodManager im = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (im != null) {
                view.requestFocus();
                im.showSoftInput(view, 0);
            }
        }
    }


}